package org.nokatag.tagjava;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.Tag;

import org.nokatag.elfunction.ELFunction;
import org.nokatag.system.Concest;

public class NokaTagImp implements NokaTagInterface{

	public  boolean isend(HttpServletRequest request,String key){
		if(request.getAttribute(key)==null || request.getAttribute(key).equals(false)){
			request.setAttribute(key,true);
			return true;
		}else
			return false;
	}
	
	public String potjs(HttpServletRequest request){
		String str="";
		if(isend(request,HTML_EDIT_PAGE_POTJS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/prototype/prototype-1.6.0.3.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/tab/scripts/dhtmlxcommon.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<link rel=\"stylesheet\" type=\"text/css\" href=\""+basepath(request)+"/"+Concest.PUBLIC_STYLE+"?v="+Concest.VERSION+"\">\n";
		}
		return str;
	}
	
	public String nhtmlboxjs(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_HTML_BOX_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/tinybox/nokahtmlbox.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	public String nkUtiljs(HttpServletRequest request){
		String str="";
		if(isend(request,FORM_HTML_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/formcheck/scripts/nk_noka_util.js?v="+Concest.VERSION+"\"></script>";
		}
		return str;
	}
	
	public String dbjs(HttpServletRequest request){
		String str="";
		if(isend(request,DBGRID_EDIT_PAGE_ISEND)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/nokatag/dbgrid/nkgrid.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String treejs(HttpServletRequest request){
		String str="";
		if(isend(request,TREE_JS_AND_CSS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/tree/ntree.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<link rel=\"stylesheet\" type=\"text/css\" href=\""+basepath(request)+"/nokatag/org/noka/tree/dtree.css?v="+Concest.VERSION+"\">\n";
		}
		return str;
	}
	
	public String treeTranjs(HttpServletRequest request){
		String str="";
		if(isend(request,TREE_TRAN_JS_AND_CSS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/tree/ntreetran.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<link rel=\"stylesheet\" type=\"text/css\" href=\""+basepath(request)+"/nokatag/org/noka/tree/dtreetran.css?v="+Concest.VERSION+"\">\n";
		}
		return str;
	}
	
	
	public String selectCss(HttpServletRequest request){
		String str="";
		if(isend(request,SELECT_LIST_CSS)){
			str+="<link rel=\"stylesheet\" type=\"text/css\" href=\""+basepath(request)+"/nokatag/org/noka/select/nkselect.css?v="+Concest.VERSION+"\">\n";
		}
		return str;
	}
	
	
	
	public String accordionCss(Integer styleid,HttpServletRequest request){
		String str="";
		if(isend(request,MENU_ACCORDION_CSS+"_"+styleid)){
			str+="<link rel=\"stylesheet\" type=\"text/css\" href=\""+basepath(request)+"/nokatag/org/noka/accordion/nk_"+styleid+"_naccordion.css?v="+Concest.VERSION+"&syid="+styleid+"\" rel=\"stylesheet\">\n";
		}
		return str;
	}
	
	
	public String selectjs(HttpServletRequest request){
		String str="";
		if(isend(request,SELECT_LIST_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/select/nkselect.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String topToolsJS(HttpServletRequest request,String style){
		String str="";
		if(isend(request,TOP_TOOLS_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/nokatag/accordion/nokatop.js?v="+Concest.VERSION+"\"></script>\n";
			if(null==style){
				str+="<link rel=\"stylesheet\" href=\""+basepath(request)+"/nokatag/org/nokatag/accordion/toptools.css?v="+Concest.VERSION+"\" />\n";
			}
		}
		if(null!=style){
			str+="<link rel=\"stylesheet\" href=\""+style+"\" />\n";
		}
		return str;
	}
	
	public String leftMenuJS(HttpServletRequest request,String style){
		String str="";
		if(isend(request,LEFT_MENU_JS_AND_CSS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/nokatag/accordion/nokaaccordion.js?v="+Concest.VERSION+"\"></script>\n";
			if(null==style){
				str+="<link rel=\"stylesheet\" href=\""+basepath(request)+"/nokatag/org/nokatag/accordion/leftMenu.css?v="+Concest.VERSION+"\" />\n";
			}
		}
		if(null!=style){
			str+="<link rel=\"stylesheet\" href=\""+style+"\" />\n";
		}
		return str;
	}
	
	public String ntabMenuJS(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_TAB_MENU_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/nokatabmenu/nktabmenu.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String ntabJS(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_TAB_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/nokatabmenu/nktab.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String selectinputjs(HttpServletRequest request){
		String str="";
		if(isend(request,SELECT_INPUT_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/select/select-input.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	
	
	public String selectMultijs(HttpServletRequest request){
		String str="";
		if(isend(request,SELECT_MULTI_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/select/select-multi.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String selectCustomjs(HttpServletRequest request){
		String str="";
		if(isend(request,SELECT_CUSTOM_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/select/select-custom.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String selectDoublejs(HttpServletRequest request){
		String str="";
		if(isend(request,SELECT_DOUBLE_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/select/select-double.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String selectButtonjs(HttpServletRequest request){
		String str="";
		if(isend(request,SELECT_BUTTON_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/select/select-button.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	
	public String inputTextJS(HttpServletRequest request){
		String str="";
		if(isend(request,INPUT_TEXT_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/formcheck/scripts/nkinput.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String ajaxinputTextJS(HttpServletRequest request){
		String str="";
		if(isend(request,AJAX_INPUT_TEXT_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/formcheck/scripts/ajaxinputtext.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	

	public String multInputTextJS(HttpServletRequest request){
		String str="";
		if(isend(request,MULT_INPUT_TEXT_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/formcheck/scripts/multinputtext.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String nkrchecksJS(HttpServletRequest request){
		String str="";
		if(isend(request,CHECKS_RADIO_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/select/nkrchecks.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String nkdateJS(HttpServletRequest request){
		String str="";
		if(isend(request,DATE_PICKER_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/datepicker/scripts/WdatePicker.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/datepicker/scripts/ndateutil.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String nkBoxJsAndCss(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_BOX_JSCSS)){
			//str="<link rel=\"stylesheet\" href=\""+basepath(request)+"/nokatag/org/tinybox/tinyboxstyle.css?v="+Concest.VERSION+"\" />\n";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/tinybox/packed.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/tinybox/nokabox.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String nktextareaJS(HttpServletRequest request){
		String str="";
		if(isend(request,NK_TEXTAREA_JS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/formcheck/scripts/nktextarea.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String potuploadjs(HttpServletRequest request){
		String str="";
		if(isend(request,HTML_UPLOAD_FILE_UTILJS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/file/upload/fileupload.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String potimageuploadjs(HttpServletRequest request){
		String str="";
		if(isend(request,HTML_UPLOAD_IMAGE_UTILJS)){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/file/upload/imageupload.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	public String ColorSelectJS(HttpServletRequest request){
		String str="";
		if(isend(request,COLOR_JS)){
			str="<link rel=\"STYLESHEET\" type=\"text/css\" href=\""+basepath(request)+"/nokatag/color/file/dhtmlxcolorpicker.css?v="+Concest.VERSION+"\">";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/color/file/dhtmlxcolorpicker.js?v="+Concest.VERSION+"\"></script>";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/color/file/ncolorSelect.js?v="+Concest.VERSION+"\"></script>";
		}
		return str;
	}
	public String htmlEdit(HttpServletRequest request){
		String str="";
		if(isend(request,HTML_EDIT_PAGE_ISEND) ){
			str="<link rel=\"stylesheet\" href=\""+basepath(request)+"/nokatag/htmledit/styles/style.css?v="+Concest.VERSION+"\" type=\"text/css\">\n";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/htmledit/scripts/wysiwyg.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/htmledit/scripts/wysiwyg-settings.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/htmledit/scripts/nhtmledit.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String TableJS(HttpServletRequest request){
		String str="";
		if(isend(request,TABLE_JS) ){
			str="<link rel=\"STYLESHEET\" type=\"text/css\" href=\""+basepath(request)+"/nokatag/tab/styles/dhtmlxtabbar.css?v="+Concest.VERSION+"\">";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/tab/scripts/dhtmlxtabbar.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String StatChartJS(HttpServletRequest request){
		String str="";
		if(isend(request,STAT_CHART_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/statchart/file/FusionCharts.js?v="+Concest.VERSION+"\"></script>\n";
			str+="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/statchart/file/nokacharts.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String SocketJS(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_WEB_SOCKET_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/socket/fla/nksocket.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	public String DbgridCSS(HttpServletRequest request){
		String str="";
		if(isend(request,DBGRID_STYLE_FILE) ){
			str="<link type=\"text/css\" href=\""+basepath(request)+"/"+Concest.DBGRID_STYLE+"?v="+Concest.VERSION+"\" rel=\"stylesheet\">";
		}
		return str;
	}
	
	public String InfoBoxJS(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_INFO_BOX_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/tinybox/infobox.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String formLayoutCss(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_FORM_LAYOUT_PUBLIC_CSS) ){
			str="<style type=\"text/css\">*{margin: 0;padding: 0;}</style>";
		}
		return str;
	}
	
	public String hiddensJs(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_INPUT_HIDDENS_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/formcheck/scripts/nkhiddens.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String hiddenJs(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_INPUT_HIDDEN_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/formcheck/scripts/nk_hidden.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String MousRigthMenuJS(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_MOUS_RIGTH_MENU_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/formcheck/scripts/mousright.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	
	public String codeBverJS(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_CODE_BVER_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/bver/bver.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	public String AccordionJS(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_ACCORDION_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/accordion/scripts/script.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	public String nstar(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_STAR_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/star/nstar.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	public String nswitch(HttpServletRequest request){
		String str="";
		if(isend(request,NOKA_SWITCH_JS) ){
			str="<script type=\"text/javascript\" src=\""+basepath(request)+"/nokatag/org/noka/nswitch/nswitch.js?v="+Concest.VERSION+"\"></script>\n";
		}
		return str;
	}
	//=================================================================
	
	public String basepath(HttpServletRequest request){
		return ELFunction.rooturl(request);
	}
	
	public String LenNumber(String Len){
		String[] els = new String[]{"px","em","pt","ex","pc","in","mm","cm","%"};
		if(Len==null)
			return Len;
		for(String e:els){
			if(Len.toLowerCase().endsWith(e))
				return Len.substring(0,(Len.length()-e.length()));
		}
		return Len;
	}
	
	public String LenFormat(String Len){
		String[] els = new String[]{"px","em","pt","ex","pc","in","mm","cm","%"};
		if(Len==null)
			return Len;
		for(String e:els){
			if(Len.toLowerCase().endsWith(e))
				return Len;
		}
		return Len+"px";
	}
	/**
	 * 获取form表单对象
	 * @param tag
	 * @return
	 */
	public FormHtml getTagFormHtml(Tag tag){
		FormHtml fh = null;
		if(tag!=null && tag instanceof FormHtml){
			fh = (FormHtml)tag;
		}else if(tag!=null && fh==null){
			fh=getTagFormHtml(tag.getParent());
		}
		return fh;
	}
	public Boolean Equery(String cx,String ex){
		return cx.trim().toLowerCase().equals(ex);
	}
	public static void main(String[] args){
		String Len = "90EM";
		NokaTagImp n = new NokaTagImp();
		
		System.out.print(n.LenNumber(Len));
	}
	
	//------快捷键---------------------------------------------
	public String shortcutkey(String keys, String buttonid,String jsfunction) {
		StringBuffer body = new StringBuffer();
		if (null != keys && keys.trim().length() > 0) {// 13+34+45
			String[] key = keys.split("\\+");
			body.append("<script type=\"text/javascript\">");
			body.append("Event.observe(document, 'dom:loaded', function() {");// altKey shiftKey
			body.append("Event.observe(document,'keyup',function(event){");// 失失焦点时验证其值
			switch (key.length) {
			case 1:
				body.append("if( event.keyCode==" + key[0] + " ){");
				break;
			case 2:
				body.append("if(event." + key[0] + " && event.keyCode=="+ key[1] + " ){");
				break;
			case 3:
				body.append("if(event." + key[0] + " && event." + key[1]+ " && event.keyCode==" + key[2] + " ){");
				break;
			}
			if(null!=buttonid){
				body.append("try{$('" + buttonid + "').click();}catch(e){}");
			}
			if(null!=jsfunction){
				body.append("try{"+jsfunction+"();}catch(e){}");
			}
			body.append("}");
			body.append("});");
			body.append("});");
			body.append("</script>");
		}
		return body.toString();
	}

}
